<?php

namespace App\Http\Controllers\Admin;

use App\Models\Item;
use App\Models\ItemCatlog;
use App\Models\ItemReviews;
use Illuminate\Http\Request;

class ItemController extends BaseController
{
    /**
     * 商品列表
     * @throws \Exception
     */
    public function index(){

        if ($this->isOnSubmit()) {
            $items = $this->request->post('items');
            $eventType = $this->request->post('eventType');
            if ($items && is_array($items)) {

                if ($eventType == 'delete'){
                    Item::whereIn('itemid',$items)->get()->map(function (Item $item){
                        $item->delete();
                    });
                }

                if ($eventType == 'on_sale'){
                    foreach ($items as $itemid) {
                        Item::where('itemid', $itemid)->update(['on_sale'=>1]);
                    }
                }

                if ($eventType == 'off_sale'){
                    foreach ($items as $itemid) {
                        Item::where('itemid', $itemid)->update(['on_sale'=>0]);
                    }
                }
                //移动文章
                if ($eventType == 'move'){
                    $this->move();
                    exit();
                }
                return ajaxReturn(['return_code'=>0]);
            }else {
                return ajaxError(1, trans('no_select'));
            }
        }else {
            $this->assign([
                'shop_name'=>'',
                'seller_name'=>'',
                'sale_status'=>'',
                'title'=>'',
                'min_price'=>'',
                'max_price'=>'',
                'itemid'=>'',
                'catid'=>''
            ]);
            $queryParams = [];
            $query = Item::with('catlog');

            $shop_name = $this->request->input('shop_name');
            if ($shop_name) {
                $condition[] = ['s.shop_name', 'LIKE', "%$shop_name%"];
                $queryParams['shop_name'] = $shop_name;
                $query = $query->whereHas('shop', function ($query) use ($shop_name){
                    return $query->where('shop_name', 'LIKE', "%$shop_name%");
                });
            } else {
                $query = $query->with('shop');
            }

            $seller_name = $this->request->get('seller_name');
            if ($seller_name) {
                $queryParams['seller_name'] = $seller_name;
                $query = $query->where('username', 'LIKE', "%$seller_name%");
            }

            $sale_status = $this->request->get('sale_status');
            if ($sale_status) {
                $queryParams['sale_status'] = $sale_status;
                if ($sale_status == 'on_sale'){
                    $query = $query->where('on_sale', 1);
                }
                if ($sale_status == 'off_sale'){
                    $query = $query->where('on_sale', 0);
                }
            }

            $title = $this->request->get('title');
            if ($title) {
                $queryParams['title'] = $title;
                $query = $query->where('title', 'LIKE', "%$title%");
            }

            $min_price = $this->request->get('min_price');
            $max_price = $this->request->get('max_price');
            if ($min_price) {
                $queryParams['min_price'] = $min_price;
                $query = $query->where('price', '>', floatval($min_price));
            }

            if ($max_price) {
                $queryParams['max_price'] = $max_price;
                $query = $query->where('price', '<', floatval($max_price));
            }

            $itemid = $this->request->get('itemid');
            if ($itemid) {
                $queryParams['itemid'] = $itemid;
                $query = $query->where('itemid', $itemid);
            }

            $catid = $this->request->get('catid');
            if ($catid) {
                $queryParams['catid'] = $catid;
                $Ids = ItemCatlog::fetchAllChildIds($catid);
                $query = $query->whereIn('catid', $Ids);
            }
            $this->assign($queryParams);

            $itemlist = $query->orderByDesc('itemid')->paginate(20);
            $this->assign([
                'itemlist'=>$itemlist,
                'pagination'=>$itemlist->appends($queryParams)->render(),
                'catlogOptions'=>ItemCatlog::getOptions($catid)
            ]);

            return $this->view('admin.item.list');
        }
    }

    /**
     * 批量删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchDelete(Request $request)
    {
        $items = $request->input('items');
        if (is_array($items))
        {
            Item::whereIn('itemid', $items)->get()->map(function (Item $item){
                $item->delete();
            });
        }

        return ajaxReturn();
    }

    /**
     * 批量上架
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchOnSale(Request $request)
    {
        $items = $request->input('items');
        if (is_array($items))
        {
            Item::whereIn('itemid', $items)->update(['on_sale'=>1]);
        }

        return ajaxReturn();
    }

    /**
     * 批量下架
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchOffSale(Request $request)
    {
        $items = $request->input('items');
        if (is_array($items))
        {
            Item::whereIn('itemid', $items)->update(['on_sale'=>0]);
        }

        return ajaxReturn();
    }

    /**
     * 批量移动
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchMove(Request $request)
    {
        $target = intval($request->input('target'));
        $items = $request->input('items');

        if (is_array($items))
        {
            Item::whereIn('itemid', $items)->update(['catid'=>$target]);
        }

        return ajaxReturn();
    }

    public function toggleBest(Request $request)
    {
        $items = $request->input('items');
        $is_best = $request->input('is_best');
        if (is_array($items))
        {
            Item::whereIn('itemid', $items)->update(['is_best'=>$is_best]);
        }

        return ajaxReturn();
    }

    public function reviews(Request $request)
    {
        $items = ItemReviews::with(['item', 'user'])->orderByDesc('id')->paginate(20);

        return $this->view('admin.item.reviews', [
            'items'=>$items,
            'pagination'=>$items->render()
        ]);
    }

    public function delReviews(Request $request)
    {
        $delete = $request->input('delete', []);
        if ($delete)
        {
            ItemReviews::whereIn('id', $delete)->delete();
        }

        return ajaxReturn();
    }
}
